package com.gs.cd.service.resource.controller;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.diboot.core.binding.Binder;
import com.gs.cd.api.ApiResult;
import com.gs.cd.model.resource.SysResource;
import com.gs.cd.model.resource.SysResourceGroup;
import com.gs.cd.model.resource.SysResourceGroupOwnResource;
import com.gs.cd.service.resource.service.SysResourceGroupOwnResourceService;
import com.gs.cd.service.resource.service.SysResourceGroupService;
import com.gs.cd.service.resource.service.SysResourceService;
import com.seven.mybatisx.controller.AbstractController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * api接口
 */
@RestController
@RequestMapping(value = {"v1/resource/{saleCode}", "resource/v1/resource/{saleCode}"})
public class ApiController {

    @Autowired
    private SysResourceService sysResourceService;
    @Autowired
    private SysResourceGroupService sysResourceGroupService;
    @Autowired
    private SysResourceGroupOwnResourceService sysResourceGroupOwnResourceService;

    /**
     * [资源]获取树结构的资源
     *
     * @param saleCode
     * @return
     */
    @GetMapping("resource/listTree")
    public ApiResult treeListAllResource(@PathVariable String saleCode) {
        if (StrUtil.isEmpty(saleCode)) {
            return ApiResult.error("saleCode不能为空值");
        }
        return ApiResult.success(sysResourceService.listTreeBySaleCode(saleCode));
    }

    /**
     * [资源]获取资源
     *
     * @param saleCode
     * @return
     */
    @GetMapping("resource/list")
    public ApiResult listAllResource(@PathVariable String saleCode, SysResource sysResource) {
        if (StrUtil.isEmpty(saleCode)) {
            return ApiResult.error("saleCode不能为空值");
        }
        Map<String, Object> stringObjectMap = AbstractController.clownCamelCase2UnderlineCase(BeanUtil.beanToMap(sysResource));
        return ApiResult.success(sysResourceService.listByMap(stringObjectMap));
    }

    /**
     * [资源] 获取根资源
     *
     * @param saleCode
     * @return
     */
    @GetMapping("resource/listRoot")
    public ApiResult listRootResource(@PathVariable String saleCode) {
        if (StrUtil.isEmpty(saleCode)) {
            return ApiResult.error("saleCode不能为空值");
        }
        return ApiResult.success(sysResourceService.listOnlyRoot(saleCode));
    }

    /**
     * [资源组]获取树结构的资源
     *
     * @param saleCode
     * @return
     */
    @GetMapping("resourceGroup/listTree")
    public ApiResult treeListAllResourceGroup(@PathVariable String saleCode) {
        if (StrUtil.isEmpty(saleCode)) {
            return ApiResult.error("saleCode不能为空值");
        }
        return ApiResult.success(sysResourceGroupService.listRoot(saleCode));
    }

    /**
     * [资源组]获取资源
     *
     * @param saleCode
     * @return
     */
    @GetMapping("resourceGroup/list")
    public ApiResult ListAllResourceGroup(SysResourceGroup sysResourceGroup, @PathVariable String saleCode) {
        if (StrUtil.isEmpty(saleCode)) {
            return ApiResult.error("saleCode不能为空值");
        }
        Map<String, Object> stringObjectMap = AbstractController.clownCamelCase2UnderlineCase(BeanUtil.beanToMap(sysResourceGroup));
        return ApiResult.success(sysResourceGroupService.listByMap(stringObjectMap));
    }

    /**
     * [资源组] 获取根资源组
     *
     * @param saleCode
     * @return
     */
    @GetMapping("resourceGroup/listRoot")
    public ApiResult listRootResourceGroup(@PathVariable String saleCode) {
        if (StrUtil.isEmpty(saleCode)) {
            return ApiResult.error("saleCode不能为空值");
        }
        return ApiResult.success(sysResourceGroupService.listOnlyRoot(saleCode));
    }

    /**
     * 通过资源组 ids 获取相应资源
     * ids 为空表示查询所有
     */
    @GetMapping("resourceGroup/resource/groupIds")
    public ApiResult resourceByGroupIds(@PathVariable String saleCode, @RequestParam List<String> ids) {
        if (StrUtil.isEmpty(saleCode)) {
            return ApiResult.error("saleCode不能为空值");
        }
        return ApiResult.success(sysResourceGroupService.listWithResourceByIds(saleCode, ids));
    }

    /**
     * 通过资源组codes 获取相应资源
     *
     * @param saleCode
     * @param codes
     * @return
     */
    @GetMapping("resourceGroup/resource/groupCodes")
    public ApiResult resourceByGroupCodes(@PathVariable String saleCode, @RequestParam List<String> codes) {
        if (StrUtil.isEmpty(saleCode)) {
            return ApiResult.error("saleCode不能为空值");
        }
        List<SysResourceGroup> list = sysResourceGroupService.listByCodes(saleCode, codes);
        List<String> collect = list.stream().map(SysResourceGroup::getId).collect(Collectors.toList());
        return ApiResult.success(sysResourceGroupService.listWithResourceByIds(saleCode, collect));
    }
}
